// index.js
import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/components/Login.vue'
import Home from '@/components/Home.vue'
import Welcome from '@/components/Welcome.vue'
import { getToken } from '@/util/SetTime';
import UserList from '@/components/UserList.vue'

import RoleList from '@/components/RoleList.vue'

import PermissionList from '@/components/PermissionList.vue';
//商品管理



import FenleiCanshu from '@/components/goods/GoodFenlei.vue';

import GoodCate from '@/components/goods/GoodCate.vue';

import GoodList from '@/components/goods/GoodList.vue';

Vue.use(Router)
const router = new Router({
    routes: [
        {
            path: '/login',
            component: Login
        },
        {
            path: '/',
            component: Login
        },

        {
            path: '/home',
            component: Home,
            redirect: "/welcome",
            children: [{
                path: '/welcome',
                component: Welcome,
            },
            {
                path: '/userList',
                component: UserList,
            },
            {
                path: '/roleList',
                component: RoleList,
            },
            {
                path: '/permissionList',
                component: PermissionList,
            }

                ,
            {
                path: '/fenleiCanshu',
                component: FenleiCanshu,
            },
            {
                path: '/goodCate',
                component: GoodCate,
            }
                ,
            {
                path: '/goodList',
                component: GoodList,
            }
            ]
        }



    ],
    mode: 'history'
})

router.beforeEach((to, from, next) => {
    // to: 即将要进入的目标路由对象
    // from: 当前导航正要离开的路由对象
    // next: 必须调用该方法来 resolve 这个钩子

    // 示例：检查用户是否登录
    if (to.path == "/login") {
        return next();
    } else {
        const token = getToken("token");

        console.log(token)
        if (!token) {
            // 到登录页面
            return next("/login");
        } else {
            //放行
            return next();
        }


    }

});



export default router